<?php

class AdminController extends CController
{
    public $layout = '/layouts/admin';

    public $breadcrumbs = array();
    private $rule = array();
    public $controller_id = '';
    public $action_id = '';
    public $referer = '';
    public $page = '';
    public $admin_name = '';
    public $sp = '';
    public $user_sp = '';
    public $nav_type = 'cgm';
    public $nav_type_list = array('cgm' => '车货匹配', 'tms' => 'TMS', 'offcial' => '官网');

    public function beforeAction($action)
    {
        parent::beforeAction($action);
        $this->controller_id = $this->getId();
        $this->action_id = $this->getAction()->getId();
        $this->referer = Yii::app()->request->getUrlReferrer();
        $this->visitCheck();
        $this->page = Yii::app()->request->getParam('page');
        $nav_type = $this->getQuery('nav_type');
        if (!empty($nav_type)) {
            if ($nav_type == 'cgm') {
                Yii::app()->user->setState('admin_nav_type', $nav_type);
                $this->nav_type = 'cgm';
            } else if ($nav_type == 'tms') {
                Yii::app()->user->setState('admin_nav_type', $nav_type);
                $this->nav_type = 'tms';
            } else if ($nav_type == 'offcial') {
                Yii::app()->user->setState('admin_nav_type', $nav_type);
                $this->nav_type = 'offcial';
            }
        } else {
            $nav_type = Yii::app()->user->getState('admin_nav_type', 'cgm');
            if (!empty($nav_type)) {
                $this->nav_type = $nav_type;
            }
        }
        return true;
    }

    /**
     * 校验访问权限
     * @return [type] [description]
     */
    private function visitCheck()
    {

        $this->rule['guest'] = array(
            'manager'=>array('login', 'logout','mypass'),
            'user'=>array('shoplist'),
            'site'=>array('verify', 'error','index'),
        );
        $this->rule['whitelist'] = array(
            'user'=>array('shoplist'),
            'goods'=>array('list'),
            'category'=>array('search'),
            'tag'=>array('search'),
        );
        if(Yii::app()->user->getIsGuest() && $this->controller_id != 'manager' && $this->action_id!='login'){
            $this->showMessage('请先登陆','/admin/manager/login');
        }

        $this->user_sp = Manager::model()->getArrById(Yii::app()->user->getId());
        $this->sp = Manager::model()->sp();
        $this->admin_name = Yii::app()->user->getName();
        if(empty($this->user_sp) || (!empty($this->user_sp) && empty($this->user_sp['sp']))){
            $this->user_sp['sp'] = array();
        } else {
            $this->user_sp = $this->user_sp['sp'];
        }
        $white_list = array('site','default');
        if ($this->controller_id =='manager' || $this->controller_id =='site' && in_array($this->action_id, $this->rule['guest'][$this->controller_id])) {
            return true;
        }

        if(Yii::app()->user->getIsGuest()){
            if(empty($this->rule['guest'][$this->controller_id]) || !in_array($this->action_id,$this->rule['guest'][$this->controller_id])) {
                $this->showMessage('请先登陆','/admin/manager/login');
            }
        }
        $name = Yii::app()->user->getName();
        if ($name != 'admin') {
            if (!empty($this->rule['whitelist'][$this->controller_id])) {
                if (!in_array($this->action_id, $this->rule['whitelist'][$this->controller_id])) {

                } else {
                    return true;
                }
            }
            $user_info = Manager::model()->getArrById(Yii::app()->user->getId());
            //用户所有允许访问的控制器
            $user_controller_id = array();
            $special_controller = Manager::model()->sp();
            $temp = array();
            if(!empty($special_controller) && is_array($special_controller))
            {
                foreach ($special_controller as $key => $value) {
                    if (!empty($value)) {
                        foreach ($value as $k => $v) {
                            $temp[] = $k;
                        }
                    }
                }
            }
            $special_controller = $temp;
            if(!empty($user_info['sp'])){
                foreach($user_info['sp'] as $s_k=>$s_v){
                    $con_id = '';
                    $explode = explode('/', $s_v);
                    $con_id = $explode[0];
                    $user_controller_id[$con_id] = $con_id;
                }
            }
            if(!in_array($this->controller_id,$user_controller_id)){
                echo '非法访问，请检查权限!';exit;
            }else{
                $sp = $user_info['sp'];
                if(in_array($this->controller_id.'/'.$this->action_id,$special_controller)){
                    if (!in_array($this->controller_id, $white_list) && !in_array($this->controller_id.'/'.$this->action_id, $sp)) {
                        echo '非法访问,请检查权限!';exit;
                    }else{
                        return true;
                    }
                }else{
                    return true;
                }

            }


        }
    }

    public function showSuccess($msg, $url = '')
    {
        $message = array();
        $message['type'] = 'success';
        $message['msg'] = $msg;
        Yii::app()->user->setFlash('alertmsg', $message);
        if (!empty($url)) {
            $this->redirect($url);
        }
    }
    public function showError($msg, $url = '')
    {
        $message = array();
        $message['type'] = 'error';
        $message['msg'] = $msg;
        Yii::app()->user->setFlash('alertmsg', $message);
        if (!empty($url)) {
            $this->redirect($url);
        }
    }

    public function showMessage($msg, $url = '',$type=false)
    {
        $message = array();
        $message['type'] = $type;
        $message['msg'] = $msg;
        Yii::app()->user->setFlash('alertmsg', $message);
        if (!empty($url)) {
            $this->redirect($url);
        }
    }
    /**
     * 保存前
     * @return [type] [description]
     */
    public function beforeSave()
    {
        $schema = $this->getTableSchema();
        if ($this->getIsNewRecord()) {
            if (in_array('ctime', array_keys($schema->columns))) {
                $this->ctime = time();
            }
            if (in_array('sort', array_keys($schema->columns))) {
                $this->sort = time();
            }
            if (in_array('mtime', array_keys($schema->columns))) {
                $this->mtime = time();
            }
        } else {
            $this->mtime = time();
        }
        return true;
    }

    public function getPost($name, $default = null)
    {
        return Yii::app()->request->getPost($name, $default);
    }

    public function getQuery($name, $default = null)
    {
        return Yii::app()->request->getQuery($name, $default);
    }
}
